package com.lkx.web;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lkx.domain.Api;
import com.lkx.domain.ApiAndType;
import com.lkx.domain.ApiParams;
import com.lkx.domain.ApiSubject;
import com.lkx.domain.User;
import com.lkx.service.ApiParamsService;
import com.lkx.service.ApiService;
import com.lkx.service.ApiSubjectService;
import com.lkx.util.PageModule;

@Controller
@RequestMapping("/back/api")
public class BackApiController {

    @Autowired
    private ApiParamsService apiParamsService;
    @Autowired
    private ApiSubjectService apiSubjectService;
    @Autowired
    private ApiService apiService;

    @RequestMapping("apiList")
    public ModelAndView apiList(Long subjectId, HttpSession session) {
        ModelAndView view = new ModelAndView("back/index");
        User user = (User) session.getAttribute("user");

        List<ApiSubject> subjectList = apiSubjectService.list(user.getId()).getList();

        List<ApiAndType> list = new ArrayList<ApiAndType>();
        list = apiService.selectApiListByOption(user.getId(), subjectId);

        Set set = new HashSet();
        for (ApiAndType apiAndType : list) {
            set.add(apiAndType.getApiType());
        }
        view.addObject("typeList", set);
        view.addObject("apiList", list);
        view.addObject("subjectList", subjectList);
        return view;
    }

    @RequestMapping("toList")
    public String toList() {
        return "api-list";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageModule list(HttpSession session, Long subjectId, String apiName, int pageNum, int pageSize) {
        PageModule module = new PageModule();
        User user = (User) session.getAttribute("user");
        module = apiService.list(user.getId(), apiName, subjectId, pageNum, pageSize);
        return module;
    }

    @RequestMapping("toAdd")
    public String toAdd() {
        return "api-add";
    }

    @RequestMapping("add")
    @ResponseBody
    public PageModule add(Api record, String params) {
        PageModule module = new PageModule();
        module = apiService.add(record, params);
        return module;
    }

    @RequestMapping("toShow")
    public ModelAndView toShow(Long id) {
        ModelAndView view = new ModelAndView("api-show");
        Api api = apiService.selectByPrimaryKey(id);
        view.addObject("api", api);
        return view;
    }

    @RequestMapping("show")
    @ResponseBody
    public PageModule show(Long id) {
        PageModule module = new PageModule();
        List<ApiParams> list = apiParamsService.selectByApiId(id);
        if (list != null && list.size() > 0) {
            module.setStatus(true);
            module.setList(list);
        }
        return module;
    }

    @RequestMapping("toEdit")
    public ModelAndView toEdit(Long id) {
        ModelAndView view = new ModelAndView("api-edit");
        Api api = apiService.selectByPrimaryKey(id);
        List<ApiParams> list = apiParamsService.selectByApiId(id);
        view.addObject("api", api);
        view.addObject("paramsList", list);
        return view;
    }

    @RequestMapping("edit")
    @ResponseBody
    public PageModule edit(Api record, String params) {
        PageModule module = new PageModule();
        try {
            module = apiService.edit(record, params);
        } catch (Exception e) {

        }
        return module;
    }

    @RequestMapping("delete")
    @ResponseBody
    public PageModule delete(Long id) {
        PageModule module = new PageModule();
        module = apiService.deleteById(id);
        return module;
    }

}
